<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/css/plugins/layui/css/layui.css}">
    <style>
        body {
            padding: 10px;
        }

        .layui-form-item.layui-inline {
            margin: 10px 0px;
            padding-left: 15px;
        }

        #condition,
        #tableDiv {
            background: #fff;
        }
    </style>
</head>

<body>
<form id="condition">
    <div class="layui-form-item">
        <div class="layui-inline">
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" placeholder="请输入专业名称" id="majorName" name="majorName" class="layui-input">
            </div>
            <button type="button" id="search" class="layui-btn">查询</button>
            <button type="reset" id="reset" class="layui-btn">重置</button>
        </div>
    </div>
</form>

<div id="tableDiv">
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-sm" lay-event="add">
        <i class="glyphicon glyphicon-plus"></i>
        新增
    </button>
    <button class="layui-btn layui-btn-sm" lay-event="update">
        <i class="glyphicon glyphicon-pencil"></i>
        修改
    </button>
    <button class="layui-btn layui-btn-sm" lay-event="del">
        <i class="glyphicon glyphicon-remove"></i>
        删除
    </button>
</script>
<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="more">
            更多
            <i class="layui-icon layui-icon-down"></i>
        </a>
    </div>
</script>
<script th:src="@{/js/plugins/layui/layui.js}"></script>
<script>
    layui.use(function () {
        var table = layui.table;
        var dropdown = layui.dropdown;
        var $ = layui.$;

        // 创建渲染实例
        table.render({
            elem: '#test',
            url: '/major/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                // 右上角工具图标
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips',
                onClick: function (obj) {
                    // 2.9.12+
                    layer.alert('自定义工具栏图标按钮');
                }
            }],
            css: [
                // 重设当前表格样式
                '.layui-table-tool-temp{padding-right: 145px;}'
            ].join(''),
            cellMinWidth: 80,
            totalRow: true,
            page: true,
            // 不允许两个中括号
            cols: [
                [
                    { type: 'checkbox', fixed: 'left' },
                    { field:'majorId', fixed: 'left', title: 'ID', width: 80, totalRow: '合计：', sort: true },
                    { field:'majorName', title: '专业名称' },
                    { fixed: 'right', title: '操作', width: 134, minWidth: 125, templet: '#toolDemo' }
                ]
            ],
            error: function (res, msg) {
                console.log(res, msg)
            }
        });

        // 工具栏事件
        table.on('toolbar(test)', function (obj) {
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var othis = lay(this);
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '添加专业',
                        type: 2,
                        area: ['70%', '80%'],
                        content: '/major/toadd',
                        fixed: false,
                        maxmin: true,
                        shadeClose: true,
                        btn: ['立即提交', '取消'],
                        btnAlign: 'c',
                        yes: function (index, layero) {
                            // 获取iframe的窗口对象
                            var iframeWin = window[layero.find('iframe')[0]['name']];
                            let majorName = iframeWin.$('#majorName');

                            if ($.trim(majorName.val()) === '') {
                                layer.msg('专业名称不能为空');
                                return majorName.focus();
                            }

                            $.ajax({
                                url: '/major/addSave',
                                type: 'post',
                                data: {
                                    majorName: majorName.val()
                                },
                                dataType: 'json',
                                success: function (res) {
                                    layer.close(index);
                                    if (res.code === 0) {
                                        layer.msg(res.msg);
                                        table.reload('test');
                                    }
                                }
                            })
                        }
                    });
                    break;
                case 'update':
                    if (id == null) {
                        return layer.msg('请选择一行');
                    }
                    layer.open({
                        title: '编辑',
                        type: 2,
                        area: ['70%', '80%'],
                        content: '/major/toupdate/' + checkStatus.data[0].majorId,
                        fixed: false,
                        maxmin: true,
                        shadeClose: true,
                        btn: ['立即提交', '取消'],
                        btnAlign: 'c',
                        yes: function (index, layero) {
                            // 获取iframe的窗口对象
                            var iframeWin = window[layero.find('iframe')[0]['name']];
                            let majorName = iframeWin.$('#majorName');

                            if ($.trim(majorName.val()) === '') {
                                layer.msg('专业名称不能为空');
                                return majorName.focus();
                            }
                            $.ajax({
                                url: '/major/updateSave',
                                type: 'post',
                                data: {
                                    majorId: checkStatus.data[0].majorId,
                                    majorName: majorName.val()
                                },
                                dataType: 'json',
                                success: function (res) {
                                    layer.close(index);
                                    if (res.code === 0) {
                                        layer.msg(res.msg);
                                        table.reload('test');
                                    }
                                }
                            })
                        }
                    });
                    break;
                case 'del':
                    if (id == null) {
                        return layer.msg('请选择一行');
                    }
                    layer.confirm('确定删除吗？', function (index) {
                        $.ajax({
                            url: '/major/delete/' + checkStatus.data[0].majorId,
                            type: 'post',
                            dataType: 'json',
                            success: function (res) {
                                layer.close(index);
                                if (res.code === 0) {
                                    layer.msg(res.msg);
                                    table.reload('test');
                                }
                            }
                        })
                    });
            }
        });

        // 触发单元格工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    title: '编辑专业',
                    type: 2,
                    area: ['70%', '70%'],
                    content: '/major/toupdate/' + data.majorId,
                    fixed: false,
                    maxmin: true,
                    shadeClose: true,
                    btn: ['立即提交', '取消'],
                    btnAlign: 'c',
                    yes: function (index, layero) {
                        // 获取iframe的窗口对象
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        let majorName = iframeWin.$('#majorName');

                        if ($.trim(majorName.val()) === '') {
                            layer.msg('专业名称不能为空');
                            return majorName.focus();
                        }
                        $.ajax({
                            url: '/major/updateSave',
                            type: 'post',
                            data: {
                                majorId: data.majorId,
                                majorName: majorName.val()
                            },
                            dataType: 'json',
                            success: function (res) {
                                layer.close(index);
                                if (res.code === 0) {
                                    layer.msg(res.msg);
                                    table.reload('test');
                                }
                            }
                        })
                    }
                });
            } else if (obj.event === 'more') {
                // 更多 - 下拉菜单
                dropdown.render({
                    elem: this,
                    show: true,
                    data: [{
                        title: '查看',
                        id: 'detail'
                    }, {
                        title: '删除',
                        id: 'del'
                    }],
                    click: function (menudata) {
                        if (menudata.id === 'detail') {
                            layer.open({
                                title: '查看专业',
                                type: 2,
                                area: ['70%', '70%'],
                                content: '/major/toinfo/' + data.majorId,
                                fixed: false,
                                maxmin: true,
                                shadeClose: true,
                                btn: ['关闭'],
                                btnAlign: 'c'
                            });
                        } else if (menudata.id === 'del') {
                            layer.confirm('确定删除吗？', function (index) {
                                $.ajax({
                                    url: '/major/delete/' + data.majorId,
                                    type: 'post',
                                    dataType: 'json',
                                    success: function (res) {
                                        layer.close(index);
                                        if (res.code === 0) {
                                            layer.msg(res.msg);
                                            table.reload('test');
                                        }
                                    }
                                })
                            });
                        }
                    },
                    align: 'right',
                    style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);'
                })
            }
        });

        $("#search").click(function () {
            var majorName = $("#majorName").val();
            var data = { majorName: majorName };
            table.reload('test', {
                where: data
            })
        })
        $("#reset").click(function () {
            table.reload('test', {
                where: {}
            });
        })
    });
</script>
</body>

</html>